package services

import (
	"context"
	"wxapi/internal/svc"
	"wxapi/models"

	"gorm.io/gorm"
)

func GetEvaluateCountByPoint(ctx context.Context, svcCtx *svc.ServiceContext, doctor_id, point int64, isLess bool) (int64, error) {
	where := "point >= ?"
	if isLess {
		where = "point < ?"
	}

	count := int64(0)
	svcCtx.Mysql.Model(&models.OrderEvaluate{}).Where(where, point).Count(&count)
	return count, nil
}

func GetEvaluatesByDoctorId(ctx context.Context, svcCtx *svc.ServiceContext, doctor_id, page, pageSize int64) ([]*models.OrderEvaluate, error) {
	var evaluates []*models.OrderEvaluate
	svcCtx.Mysql.Model(&models.OrderEvaluate{}).Preload("User").Preload("User.UserInfo").Preload("EvaluateImage", func(db *gorm.DB) *gorm.DB {
		return db.Where("state = 100")
	}).Where("doctor_id = ? AND state = 100", doctor_id).Order("created_at desc").Offset(int(page) * int(pageSize)).Limit(int(pageSize)).Find(&evaluates)
	return evaluates, nil
}
